<?php
class Controller_WWW_ScenicSpot extends Controller_WWW_Template
{
    public function __construct ($req, $res)
    {
        $this->template = "smarty:www/scenicspot/index";
        parent::__construct($req, $res);
    }

    public function action_index ($page = 1)
    {
        $this->action_search($page);
    }

    // 用于在路线中创建自定义景点
    public function action_iframe_add ($trackid)
    {
        $this->template->set_filename('smarty:www/scenicspot/form_add_new');
        if (empty($this->_post))
        {
        }
        else
        {
            list($thumbUrl, $thumbPos)   = Media::save_upload_file('thumb', Kohana::config('env.image.scenic_thumb'));
            list($bannerUrl, $bannerPos) = Media::save_upload_file('cover', Kohana::config('env.image.scenic_banner'));
            
            if($_FILES['images']){
            	$cou=count($_FILES['images']['name']);
            	for($i = 0; $i < $cou ;$i++){
            		if($_FILES['images']['name'][$i]){
            			$file_arr[$i]['name'] = $_FILES['images']['name'][$i];
            			$file_arr[$i]['type'] = $_FILES['images']['type'][$i];
            			$file_arr[$i]['tmp_name'] = $_FILES['images']['tmp_name'][$i];
            			$file_arr[$i]['error'] = $_FILES['images']['error'][$i];
            			$file_arr[$i]['size'] = $_FILES['images']['size'][$i];
            		}
            	}
            }
            
            foreach($file_arr as $k => $v) {
            	$_FILES['images'] = $v;
            	list($imgUrl, $imgPos) = Media::save_upload_file('images', Kohana::config('env.image.scenic_images'));
            	if(!empty($imgUrl)){
            		$imgs[]=trim($imgUrl);
            	}     	 
            }
            
            $track = new App_Track($trackid);
            $track_data=$track->detail();
	        $row = array(
	            'name'    => trim(Arr::get($this->_post, 'name')),
	            'address' => trim(Arr::get($this->_post, 'address')),
	        	'website' => trim(Arr::get($this->_post, 'website')),
	        	'descOpenTime' => trim(Arr::get($this->_post, 'descOpenTime')),
	        	'descTickets' => trim(Arr::get($this->_post, 'descTickets')),
	            'subTitle'=> trim(Arr::get($this->_post, 'subTitle')),
	            'summary' => Arr::get($this->_post, 'summary'),
	            'lat'     => Arr::get($this->_post, 'lat'),
	        	'lng'     => Arr::get($this->_post, 'lng'),
	            'images'  => implode('|', $imgs),
                'thumb'   => $thumbUrl,
                'cover'   => $bannerUrl,
                'city'    => $track_data['city'],
                'cityId'  => $track_data['cityId'],
	        	'type'    => 2,
                'country' => $track_data['country'],
	        );
	
	        $scenic = new ScenicSpot;
	        $id     = $scenic->addByGuider($this->_uid, $row);
	
            $this->template->set('scenic', array(
	            'name'    => $row['name'],
            	'subTitle'=> $row['subTitle'],
	            'iconUrl' => '/resource/www/imgs/icon/0.png',
	            'id'      => $id,
            	'type'    => $row['type']
	        ));
        }
    }

    public function action_search($page = 1)
    {
        $query = $this->request->query();
        $cityId = Arr::get($query, 'city');
        $city = App_City::getNameCnById($cityId, 'cityName');
        $type = Arr::get($query, 'type');

        //景点类型
        $list = App_Scenictype::getTypeListByCityId($cityId);
        //排除自定义景点
        foreach ($list as $k => $v)
        {
            if($k==2)
              unset($list[$k]);
        }
        $this->template->set('typeList',$list);
        //城市列表
        $cityList=App_City::getCityListCn();
        $this->template->set('cityList',$cityList);
        //传递搜素参数到新页面
        $this->template->set('scity',$city);
        $this->template->set('scityId',$cityId);
        $this->template->set('stype',$type);
        
        $scenic  = new ScenicSpot;
        $page   = max($page, 1);
        $limit  = 9;
        $offset = ($page - 1) * $limit;

        list($countsTotal, $items) = $scenic->search( $cityId, $type,$offset,$limit);
        $pagination = $this->pagination($countsTotal, $limit, $page_type = 'route', $page_key = 'param1', $views = 'www/page/new');
        $this->template->set('pagination', $pagination);
        //获得评论数
        $items =    Comments::getCommentsCounts($items);
        $this->template->set("data",$items);
        $this->template->set("step",'scenicspot');

    }

    public function action_detail ($nid)
    {
        $this->template->set_filename("smarty:www/scenicspot/detail");

        $scenic = new ScenicSpot($nid);
        $detail = $scenic->detail();
        $detail['images'] = explode('|',$detail['images']);
        $user = user::currentUser();
        $uid = $user->getUserUid();

        $this->template->set('scenic', $detail);
        $this->template->set('uid', $uid);
        $this->template->set('nid', $nid);
        $this->template->set("step",'scenicspot');
    }

    public function action_ajax_setlocation ($scenicId)
    {
        $post = $this->request->post();
        $lng  = Arr::get($post, 'lng');
        $lat  = Arr::get($post, 'lat');

        $scenic = new ScenicSpot($scenicId);
        $scenic->setGeoLocation($lat, $lng);

        echo json_encode(array('status' => 0));
        exit;
    }

}
